from jwt import DecodeError
from rest_framework import generics
from rest_framework.parsers import FormParser, MultiPartParser
from rest_framework.views import APIView
from file.models import *
from rest_framework_jwt.authentication import jwt_decode_handler
from rest_framework.response import Response
from file.serializers import *
from utils.response import *
from drf_yasg import openapi
from drf_yasg.utils import swagger_auto_schema
import requests
from bs4 import BeautifulSoup


class FileAPI(generics.GenericAPIView):
    parser_classes = (MultiPartParser, FormParser)
    serializer_class = FileSerializer

    File = openapi.Parameter('file', required=True, in_=openapi.IN_QUERY, description='file',
                             type=openapi.TYPE_STRING)
    Token = openapi.Parameter('token', required=True, in_=openapi.IN_QUERY, description='token',
                              type=openapi.TYPE_STRING)

    @swagger_auto_schema(
        manual_parameters=[File, Token],
        responses={
            '200': openapi.Response('', FileSerializer)
        },
        security=[],
        operation_id='上传文件接口',
        operation_description='上传文件（测试中勿用）',
    )
    def post(self, request):
        if request.method == 'POST':
            file = request.FILES.get('file', None)
            token = request.META.get('HTTP_TOKEN')
            try:
                toke_user = []
                toke_user = jwt_decode_handler(token)
            except DecodeError:
                nothing['msg'] = '上传失败'
                nothing['error'] = 'token不匹配或者没有找到token'
                return Response(nothing)
            user = toke_user["user_id"]
            if token is not None:
                if file is not None:
                    name = file.name.split('.')[-1].lower()
                    if name not in ['jpg', 'png', 'html']:
                        put['msg'] = '上传错误'
                        put['error'] = '上传文件格式错误'
                        return Response(put)
                    else:
                        up = File.objects.create(file=file, owner_id=user)
                        serializer = self.get_serializer(up)
                        response['data'] = serializer.data
                        return Response(response)
                else:
                    nothing['msg'] = '上传失败'
                    nothing['error'] = '没有找到上传文件'
                    return Response(nothing)
            else:
                nothing['msg'] = '上传失败'
                nothing['error'] = '没有找到上传人'
                return Response(nothing)


class ArticleAPI(APIView):
    @swagger_auto_schema(
        manual_parameters=[],
        responses={
            '200': openapi.Response('')
        },
        security=[],
        operation_id='测试接口',
    )
    def post(self, request):
        if request.method == 'POST':
            title = request.POST.get('title')
            data = request.POST.get('html')
            if data == '':
                return Response(response)
            else:
                return Response(nothing)

